<?php


namespace app\service;

use app\model\Result;
use think\facade\Db;

/**
 * 商品类别
 */
class CategoryService
{
    /**
     * 获取全部父类别
     */
    public static function queryAllFatherCategory() {
        $field = '*';

        $res = Db::name('father_category')->field($field)->where('status', 1)->select();

        return Result::index(200,'操作成功',$res);
    }

    /**
     * 获取该父类别下的全部子类别
     */
    public static function queryAllSonCategoryByFather($param) {
        $field = '*';

        $res =  Db::name('son_category')->field($field)->where('father_category',$param['father_category'])->where('status', 1)->select();

        return Result::index(200,'操作成功',$res);
    }

    /**
     * 新增父类别
     */
    public static function createFatherCategory($param) {

        $field = '*';

        $rs = Db::name('father_category')->field($field)->where('father_category',$param['father_category'])->where('status', 1)->find();
        if(!empty($rs)){
            return Result::index(100,'该类别已存在',$rs);
        }

        $id = Db::name('father_category')->insertGetId($param);

        return Result::index(200,'操作成功',$id);
    }

    /**
     * 新增子类别
     */
    public static function createSonCategory($param) {

        $field = '*';

        $rs = Db::name('son_category')->field($field)->where('father_category',$param['father_category'])->where('son_category',$param['son_category'])->where('status', 1)->find();
        if(!empty($rs)){
            return Result::index(100,'该类别已存在',$rs);
        }

        $id = Db::name('son_category')->insertGetId($param);

        return Result::index(200,'操作成功',$id);
    }

    /**
     * 删除父类别
     */
    public static function deleFatherCategory($param) {
        $res = Db::name('father_category')->where('father_category','=',$param['father_category'])->update(['status' => 0]);

        return Result::index(200,'操作成功',$res);
    }

    /**
     * 删除子类别
     */
    public static function deleSonCategory($param) {

        $res = Db::name('son_category')->where('father_category',$param['father_category'])->where('son_category',$param['son_category'])->update(['status' => 0]);

        return Result::index(200,'操作成功',$res);
    }
}
